
#include <vector>
#include <deque>

#include <stdio.h>

#ifndef WIN32
	#include <unistd.h> // for getcwd
#else
	#include <direct.h> // for getcwd
#endif
#include <stdlib.h>// for MAX_PATH

#include "MaKI.h"

#include "stdafx.h"

#include "utils/Settings.h"
#include "utils/Output.h"


/* version and build date info */
const std::string VERSION    = "0.6"; //TODO customize
const std::string BUILD_DATE = __DATE__ " " __TIME__;


void printApplicationInfo(){
	std::cout << std::endl;
	std::cout << "MaKi version " << VERSION << std::endl;
	std::cout << "Build: " << BUILD_DATE << std::endl; 
	std::cout << std::endl;
}



/** 
 * Print usage message.
 **/
void usage(){
	/* Do not use Debug class here, because it may not have been
	 * properly set up, and a usage message will look better
	 * this way anyways */
	 
	 std::cout << "MaKI: An application for finding transformations between aerial images" << std::endl;
	 std::cout << std::endl;
	 std::cout << "Usage: Maki <working dir> <filename pattern>" << std::endl;
	 std::cout << std::endl;	
}



//************************************
// Method:    	main
// FullName:  	main
// Access:    	public 
// Returns:   	int
// Parameter: 	int argc
// Parameter: 	char * argv[]
// Description:	Initial Main-Method
//************************************
int main(int argc, char* argv[])
{
	
	printApplicationInfo();
	
	///TODO replace with real argument handling
	//argv[1] contains path to files, 
	//argv[2] contains type of outputhandling (1=full output, 0=no debug-output, -1=no output at all
	//so argc has to be >=3
	if (argc < 3)
	{
		usage();
		
	} else {
		//LibTiff throws a bunch of pointless warnings, so it's disabled
		TIFFSetWarningHandler(NULL);
		
		Settings::readConfig("config.txt");
		
		Settings::printConfig(Output::VERBOSE);
		
		
		/* ok, let's go. */
		
		MaKI* maki = new MaKI(); 


		bool useAbsolutePath;

		useAbsolutePath = true;
		if (argc == 4){
			if(atoi(argv[3]) == 0){
				useAbsolutePath = false;
			}//in all other cases, it will be true (also, if no arg is given)
		}

		vector<std::string> fileNames;
		std::stringstream filePath;
		std::string pattern;

#ifndef WIN32
		char basePath[PATH_MAX];
		getcwd(basePath, PATH_MAX);
#else
		char basePath[MAX_PATH];
		getcwd(basePath, MAX_PATH);
#endif

	
		if(useAbsolutePath){
		
#ifndef WIN32
		filePath << basePath << "/" << argv[1] << "/";
#else
		filePath << basePath << "\\" << argv[1] << "\\";
#endif
		} else {
			filePath << argv[1] << "\\";
		}

		pattern  = argv[2];
		
		Output::message("Base path: %s", Output::DEBUG, basePath);
		Output::message("File path: %s", Output::DEBUG, filePath.str().c_str());
	
		maki->getFileNames(filePath.str(), pattern, fileNames, false);
		
		if (fileNames.size() < 2){
			Output::message("Please provide at least 2 images to process! Exiting.", Output::CRITICAL);
			
		}  else {
			Output::message("Images to process:", Output::VERBOSE);
			for (unsigned int i = 0; i < fileNames.size(); ++i) {
				Output::message("%s", Output::VERBOSE, fileNames[i].c_str());
			}
			Output::separator("", Output::VERBOSE);
	
			maki->processImages(filePath.str(), fileNames); //this is where the magic happens, baby!	
		}


	}
	return EXIT_SUCCESS;
}

